package cn.tlb.gl.game.rebuild3;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.khronos.opengles.GL10;

import cn.tlb.gl.game.rebuild2.BitmapSizeUnexpectedException;
import cn.tlb.gl.game.rebuild2.GameModel;
import cn.tlb.gl.game.rebuild2.SkinCrop64x32;

/**
 * Created by 汤林冰 on 2016/1/13 15:06.
 * <p>
 *     类中的注释描述的左右方向是这样规定的:<br/>
 *     以人对着屏幕的视角看,你的左手即左边, 右手即右边
 * </p>
 */
public class NewGameModel extends GameModel{
    private String texturePath;
    private InputStream textureStream;
    private Bitmap textureBitmap;
    public NewGameModel(Context context) {
        super(context);
    }

    public void setTexturePath(String texturePath) {
        this.texturePath = texturePath;
    }

    public void setTextureBitmap(Bitmap textureBitmap) {
        this.textureBitmap = textureBitmap;
    }

    public void setTextureStream(InputStream textureStream) {
        this.textureStream = textureStream;
    }

    public void loadTexture(GL10 gl) {
        AssetManager assetManager = context.getAssets();
        if(texturePath != null) {
            try {
                InputStream open = assetManager.open("skin/char-28.png");
                textures = new SkinCrop64x32(BitmapFactory.decodeStream(open)).genTextures(gl);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BitmapSizeUnexpectedException e) {
                e.printStackTrace();
            }
            return;
        }

        if(textureStream != null){
            try {
                textures = new SkinCrop64x32(BitmapFactory.decodeStream(textureStream)).genTextures(gl);
            }catch (BitmapSizeUnexpectedException e) {
                e.printStackTrace();
            }
            return;
        }

        if(textureBitmap != null){
            try {
                textures = new SkinCrop64x32(textureBitmap).genTextures(gl);
            }catch (BitmapSizeUnexpectedException e) {
                e.printStackTrace();
            }
        }
    }
}
